using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * 
 * Autor      : Jose Jornando de Carvalho Junior    
 * Data       : 28/03/2010 10:46:38
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace Serix
{
   public class CNiveisSetores
   {
       private string COD_NIVEL ;
       private string DSC_NIVEL ;

	   /// <sumary>
       /// Construtor da classe niveis_setores
       /// <sumary>
       public CNiveisSetores()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Niveis_setores
       /// <sumary>
       public CNiveisSetores( string COD_NIVEL, string DSC_NIVEL )
       {
          this.COD_NIVEL = COD_NIVEL;
          this.DSC_NIVEL = DSC_NIVEL;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// 
        /// <sumary>
        public string Cod_nivel 
		{
		   get
		   {
		       return this.COD_NIVEL;
		   }
		   set
		   {
		       this.COD_NIVEL = value;
		   }
		}
     	/// <sumary>
        /// 
        /// <sumary>
        public string Dsc_nivel 
		{
		   get
		   {
		       return this.DSC_NIVEL;
		   }
		   set
		   {
		       this.DSC_NIVEL = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Niveis_setores.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SEG_NIVEIS_SETORES
			                  ( COD_NIVEL, DSC_NIVEL )
						   values
						      ( :COD_NIVEL, :DSC_NIVEL )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("COD_NIVEL",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("DSC_NIVEL",OracleType.VarChar)); 
				
                command.Parameters["COD_NIVEL"].Value = this.COD_NIVEL;
                if (String.IsNullOrEmpty(this.DSC_NIVEL))
                {
                    command.Parameters["DSC_NIVEL"].Value = DBNull.Value;
                }
                else
                {
                    command.Parameters["DSC_NIVEL"].Value = this.DSC_NIVEL;
                }  
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Niveis_setores.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SEG_NIVEIS_SETORES set 
			                  ( DSC_NIVEL = :DSC_NIVEL )
						   where
						      ( COD_NIVEL = :COD_NIVEL )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("COD_NIVEL",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("DSC_NIVEL",OracleType.VarChar)); 
				
                command.Parameters["COD_NIVEL"].Value = this.COD_NIVEL;
                if (String.IsNullOrEmpty(this.DSC_NIVEL))
                {
                    command.Parameters["DSC_NIVEL"].Value = DBNull.Value;
                }
                else
                {
                    command.Parameters["DSC_NIVEL"].Value = this.DSC_NIVEL;
                }  
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Niveis_setores.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SEG_NIVEIS_SETORES", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("COD_NIVEL", DbType.Int32, this.COD_NIVEL));
            tab.delete();
        }
		
		static public DataTable ObterTodos()
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SEG_NIVEIS_SETORES order by  ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CNiveisSetores PesquisaporCodigo(string COD_NIVEL, string login)
        {
            CNiveisSetores obj;
            string SQL = @"SELECT *
                            FROM  Serix.SEG_NIVEIS_SETORES 
                            WHERE COD_NIVEL = :COD_NIVEL";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":COD_NIVEL", DbType.String, COD_NIVEL);
               
				qy.Executar();
                obj = new CNiveisSetores( qy.ObterCampo("COD_NIVEL"), qy.ObterCampo("DSC_NIVEL") );
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CNiveisSetores();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt()
        {
		    string login = "serix";
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_IDT_TER() IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }
    }
}	

